package com.example.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FormatDate {


    //dateStr=“Tue Jun 01 21:38:23 CST/GMT 2022"，但是想要的格式是"2022-06-01 21:38:23”
    public static String ConvertStringToString(String dateStr){
        if(StringUtils.isBlank(dateStr)){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
            String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            System.out.println(formatDate);
            return formatDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Date ConvertStringToDate(String dateStr){
        if(StringUtils.isBlank(dateStr)){
            return new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }


}
